//'use strict';
//var project = require('../config.js');
//var apiDomain = project.domain;
//var loginUrl = apiDomain + project.loginCgi;
//var updateInfoCgi = apiDomain + project.updateInfoCgi;

function getCode(resolve, reject){
    wx.login({
        success: function (res) {
            var sessionId = wx.getStorageSync('sessionId');
            var newTimeTut = wx.getStorageSync('time_out') || 0;
            newTimeTut =newTimeTut;
            var code = res.code;
            var oldTimeOut =new Date().getTime()/1000;
            if(sessionId && newTimeTut && newTimeTut > oldTimeOut){
                var data =  {
                    wx_app_session_key : sessionId
                }
            }else{
                var data =  {
                    wx_app_js_code : code,
                }
            }

            resolve(data);

        },
        fail:function(res){
            console.log('login.微信服务器失败')          
            // var sessionId = wx.getStorageSync('sessionId');
            // var code = res.code;
            // var data =  {
            //     code : code,
            // }
            // resolve(data);
        }
    });
}

const login = function(){
        return new Promise((resolve,reject) => {
        wx.checkSession({
            success:function(res){

                var wx_app_js_code = getCode(resolve, reject);
            },
            fail:function(res){
                var wx_app_js_code = getCode(resolve, reject);
            }
        });

    });
}

module.exports = {
  login : login
}

